package com.mcr.graduateprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcr.graduateprogram.bean.GameCategory;
import com.mcr.graduateprogram.mapper.GameCategoryMapper;
import com.mcr.graduateprogram.service.IGameCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author graduateprogram
 * @since 2023-02-25
 */
@Service
public class GameCategoryServiceImpl extends ServiceImpl<GameCategoryMapper, GameCategory> implements IGameCategoryService {
    @Autowired
    private GameCategoryMapper gameCategoryMapper;

    @Override
    public Page<GameCategory> getPage(Integer page, Integer pageSize, String name) {
        Page<GameCategory> gameCategoryPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<GameCategory> gameCategoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
        gameCategoryLambdaQueryWrapper.like(name!=null,GameCategory::getClassesName,name);
        gameCategoryMapper.selectPage(gameCategoryPage,gameCategoryLambdaQueryWrapper);
        return gameCategoryPage;
    }

    @Override
    public boolean updateGameGategoryNameById(GameCategory gameCategory) {
        List<GameCategory> gameCategoryList = checkName(gameCategory);
        if(gameCategoryList.size()==0){
            gameCategoryMapper.updateById(gameCategory);
            return true;
        }else if(gameCategoryList.size()==1&&gameCategoryList.get(0).getId().longValue()==gameCategory.getId().longValue()){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean addNewGameCategory(GameCategory gameCategory) {
        List<GameCategory> gameCategoryList = checkName(gameCategory);
        if(gameCategoryList.size()==0){
            gameCategoryMapper.insert(gameCategory);
            return true;
        }else {
            return false;
        }
    }

    private List<GameCategory> checkName(GameCategory gameCategory){
        LambdaQueryWrapper<GameCategory> gameCategoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
        gameCategoryLambdaQueryWrapper.eq(GameCategory::getClassesName,gameCategory.getClassesName());
        return gameCategoryMapper.selectList(gameCategoryLambdaQueryWrapper);
    }
}
